package org.gruposp2p.aularest.model;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.gruposp2p.aularest.server.LinkConstants;

/**
 *
 * @author jj
 */
@XmlType
@XmlRootElement(name = "address")
@XmlAccessorType(XmlAccessType.NONE)
@Entity
@Table(name = "ADDRESS", catalog = "database", schema = "PUBLIC")
@NamedQueries({@NamedQuery(name = "Address.findAll", query = "SELECT a FROM Address a"), @NamedQuery(name = "Address.findById", query = "SELECT a FROM Address a WHERE a.id = :id"), @NamedQuery(name = "Address.findByName", query = "SELECT a FROM Address a WHERE a.name = :name"), @NamedQuery(name = "Address.findByPostalcode", query = "SELECT a FROM Address a WHERE a.postalcode = :postalcode"), @NamedQuery(name = "Address.findByProvince", query = "SELECT a FROM Address a WHERE a.province = :province"), @NamedQuery(name = "Address.findByTown", query = "SELECT a FROM Address a WHERE a.town = :town")})
public class Address implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "NAME", nullable = false, length = 48)
    private String name;
    @Column(name = "POSTALCODE", length = 10)
    @XmlElement(name="postalcode")
    private String postalcode;
    @Basic(optional = false)
    @Column(name = "PROVINCE", nullable = false, length = 48)
    @XmlElement(name="province")
    private String province;
    @Basic(optional = false)
    @Column(name = "TOWN", nullable = false, length = 48)
    @XmlElement(name="town")
    private String town;
    @JoinColumn(name = "STUDENT_ID", referencedColumnName = "ID")
    @ManyToOne
    private Student studentId;
    @JoinColumn(name = "TUTOR_ID", referencedColumnName = "ID")
    @ManyToOne
    private Tutor tutorId;

    @Transient
    @XmlElement(name="addressLink")
    private Link link;

    public Link getLink () {
        return link;
    }

    public void setLink(Link link) {
        this.name = link.getName();
        this.link = link;
    }
    
    @Transient
    @XmlElement(name="tutorLink")
    private Link tutorLink;
    public Link getTutorLink() {
        return tutorLink;
    }

    @Transient
    @XmlElement(name="studentLink")
    private Link studentLink;
    public Link getStudentLink() {
        return studentLink;
    }

    public Address() { }

    public Address(Integer id) {
        this.id = id;
    }

    public Address(Integer id, String name, String province, String town) {
        this.id = id;
        this.name = name;
        this.province = province;
        this.town = town;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPostalcode() {
        return postalcode;
    }

    public void setPostalcode(String postalcode) {
        this.postalcode = postalcode;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getTown() {
        return town;
    }

    public void setTown(String town) {
        this.town = town;
    }

    public Student getStudentId() {
        return studentId;
    }

    public void setStudentId(Student studentId) {
        this.studentId = studentId;
    }

    public Tutor getTutorId() {
        return tutorId;
    }

    public void setTutorId(Tutor tutorId) {
        this.tutorId = tutorId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Address)) {
            return false;
        }
        Address other = (Address) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.gruposp2p.restletjpa.model.Address[id=" + id + "]";
    }

    @PostLoad
    public void postProcess() {
        link = new Link();
        link.setHref(LinkConstants.getAddressLink(id.toString()));
        link.setId(id.toString());
        link.setName(name);
        if (tutorId != null) {
            tutorLink = tutorId.getLink();
        }
        if (studentId != null) {
            studentLink = studentId.getLink();
        }        
    }

    @PrePersist
    public void prePersist () {
        this.name = link.getName();
    }

}
